/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

package com.cctv.esb._interface.taskchangeservice._0_1;

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Logger;
import javax.xml.bind.JAXBElement;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.apache.commons.lang.StringUtils;
import com.cctv.cdm.schema.bclistandchangeinfotype._0.BroadcastListAndChangeInfoType;
import com.cctv.esb.schema.commoncomplextype._0.ProgramBasicInfoType;
import com.cctv.esb.schema.taskdatatypes._0.TaskBasicInfoType;
import com.cctv.esb.schema.taskdatatypes._0.TaskItemModifyType;
import com.cctv.esb.schema.taskdatatypes._0.TaskSpecificInfoType;
import com.cctv.esb.schema.taskmessages._0.CommonTaskResponseType;
import com.cctv.ppm.schema.ppm_commondatatypes._0.BroadcastListChangeInfoType;
import com.cctv.ppm.schema.ppm_commondatatypes._0.BroadcastListInfoType;
import com.cctv.ppm.schema.ppm_commondatatypes._0.BroadcastListItemType;
import com.ctvit.common.EntityClassSet;
import com.ctvit.framework.web.context.ContextHolder;
import com.ctvit.wsservice.basedata.domain.ZcdmTChange;
import com.ctvit.wsservice.basedata.domain.ZcdmTChangesub;
import com.ctvit.wsservice.basedata.domain.ZcdmTProglist;
import com.ctvit.wsservice.basedata.domain.ZcdmTProrec;
import com.ctvit.wsservice.basedata.domain.ZppmTProginfo;
import com.ctvit.wsservice.basedata.domain.ZppmTask;
import com.ctvit.wsservice.basedata.service.TaskChangePortService;

/**
 * This class was generated by Apache CXF 3.1.4 2015-12-17T10:36:06.052+08:00
 * Generated source version: 3.1.4
 * 
 */

@javax.jws.WebService(serviceName = "TaskChangeHttpService", portName = "TaskChangePort", targetNamespace = "http://cctv.com/MSG/service/TaskChangeService/0.1", endpointInterface = "com.cctv.esb._interface.taskchangeservice._0_1.TaskChange")
public class TaskChangePortImpl implements TaskChange {

	private static final Logger LOG = Logger.getLogger(TaskChangePortImpl.class
			.getName());

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cctv.esb._interface.taskchangeservice._0_1.TaskChange#getServiceInfo
	 * (com.cctv.esb.schema.monitor._0.GetServiceInfoRequestType
	 * getServiceInfoRequest )*
	 */
	public com.cctv.esb.schema.monitor._0.GetServiceInfoResponseType getServiceInfo(
			com.cctv.esb.schema.monitor._0.GetServiceInfoRequestType getServiceInfoRequest) {
		LOG.info("Executing operation getServiceInfo");
		System.out.println(getServiceInfoRequest);
		try {
			com.cctv.esb.schema.monitor._0.GetServiceInfoResponseType _return = null;
			return _return;
		} catch (java.lang.Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cctv.esb._interface.taskchangeservice._0_1.TaskChange#changeTask(
	 * com.cctv.esb.schema.messagehead._0.RequestHeadType requestHead
	 * ,)com.cctv.esb.schema.taskmessages._0.ChangeTaskRequestType
	 * changeTaskRequest )*
	 */
	public com.cctv.esb.schema.taskmessages._0.CommonTaskResponseType changeTask(
			com.cctv.esb.schema.messagehead._0.RequestHeadType requestHead,
			com.cctv.esb.schema.taskmessages._0.ChangeTaskRequestType changeTaskRequest) {
		LOG.info("Executing operation changeTask");
		System.out.println(requestHead);
		System.out.println(changeTaskRequest);
		TaskChangePortService taskChangePortService = ContextHolder
				.getBean(TaskChangePortService.class);// 变更单的service方法
		TaskItemModifyType taskItemModifyType = changeTaskRequest
				.getModifiedTaskItem().get(0);// 任务特定信息类型
		int delSum = 0;// 记录删除的节目单子表条数
		String response = "";// 保存反馈信息内容
		ZppmTask zppmTask = null;
		ZcdmTChange zcdmTChange = null;// 节目变更单主表
		ZcdmTProrec zcdmTProrec = null;// 节目单主表
		ZppmTProginfo zppmTProginfo = null;
		ZcdmTProglist zcdmTProglist = null;// 节目单子表
		ZcdmTChangesub zcdmTChangesub = null;// 节目变更单子表
		EntityClassSet entityClassSet = new EntityClassSet();// 为实体类赋值的类
		com.cctv.esb.schema.taskmessages._0.CommonTaskResponseType _return = null;
		TaskBasicInfoType taskBasicInfoType = taskItemModifyType
				.getModifiedTaskBasicInfo();
		ProgramBasicInfoType programBasicInfoType = taskItemModifyType
				.getModifiedProgramBasicInfo();
		TaskSpecificInfoType taskSpecificInfoType = taskItemModifyType
				.getModifiedSpecificInfo();
		String taskid=changeTaskRequest.getModifiedTaskItem().get(0).getTaskID();
		try {
			if (taskSpecificInfoType != null) {
				if (StringUtils.equals(taskSpecificInfoType.getObjectType(),
						"BroadcastListChangeInfo")) {
					BroadcastListAndChangeInfoType bAndChangeInfoType = taskSpecificInfoType
							.getObjectEntity()
							.getBroadcastListAndChangeInfoRequest();
					JAXBElement<BroadcastListInfoType> bJaxbElement = bAndChangeInfoType
							.getBroadcastListInfo();
					BroadcastListInfoType broadcastListInfoType = bJaxbElement == null ? null
							: bJaxbElement.getValue();
					if (broadcastListInfoType != null) {
						String status = broadcastListInfoType
								.getPlaylistLockStatus();// 播出单状态
//						if (!status.equals("1")) {
//							response = "播出单状态不为1,操作失败!";
//							return setResponseMessage(response);
//						}
						broadcastListInfoType.setPlaylistLockStatus("1");// 锁定状态设为1
						zcdmTProrec = entityClassSet.setZcdmTProrec(
								broadcastListInfoType, zcdmTProrec);
						taskChangePortService.saveZcdmTProrec(zcdmTProrec);// 保存节目单主表
						response = "操作成功!";
						return setResponseMessage(response,taskid);
					}
//					if (!StringUtils.equals(requestHead.getTargetSysID(), "ES01")){
//						response = "只收媒资系统变更单!";
//						return setResponseMessage(response);
//					}
					List<BroadcastListChangeInfoType> broadcastListChangeInfoTypes = bAndChangeInfoType
							.getBroadcastListChangeInfos()
							.getBroadcastListChangeInfo();
					BroadcastListChangeInfoType broadcastListChangeInfoType = broadcastListChangeInfoTypes
							.get(0);
					/** 节目变更单主表内容 **/
					zcdmTChange = entityClassSet.setZcdmTChange(
							broadcastListChangeInfoTypes,
							taskChangePortService, zcdmTChange);
					// 在变更单主表已经存在的情况下,直接返回信息
					if (zcdmTChange == null) {
						response = "变更单主表已经存在,操作失败!!";
						_return = setResponseMessage(response,taskid);
						return _return;
					}
					/** 节目变更单子表变更前的单子表内容 **/
					List<BroadcastListItemType> broadcastListItemTypesBefore = broadcastListChangeInfoType
							.getChangeOriginItems().getChangeOriginItem();// 节目单变更前
					List<ZcdmTChangesub> ListzcdmTChangesubBefore = entityClassSet
							.setChangesubs(broadcastListChangeInfoType,
									broadcastListItemTypesBefore,
									taskChangePortService, zcdmTChangesub, true);// 保存变更前内容
					if (ListzcdmTChangesubBefore == null) {
						response = "变更前的内容不存在,操作失败!";
						_return = setResponseMessage(response,taskid);
						return _return;
					}
					/** 节目变更单子表变更后的单子表内容 **/
					List<BroadcastListItemType> broadcastListItemTypesAfter = broadcastListChangeInfoType
							.getChangeTargetItems().getChangeTargetItem();// 节目单变更后
					List<ZcdmTChangesub> ListzcdmTChangesubAfter = entityClassSet
							.setChangesubs(broadcastListChangeInfoType,
									broadcastListItemTypesAfter,
									taskChangePortService, zcdmTChangesub);
					/** 合并节目变更单前后的集合 **/
					List<ZcdmTChangesub> ListZcdmTChangesub = new ArrayList<ZcdmTChangesub>();
					ListZcdmTChangesub.addAll(ListzcdmTChangesubBefore);
					ListZcdmTChangesub.addAll(ListzcdmTChangesubAfter);
//					for (int i = 0; i < ListzcdmTChangesubBefore.size(); i++) {
//						int j = taskChangePortService
//								.deleteZcdmTProglist(ListzcdmTChangesubBefore
//										.get(i).getZProItemCd());// 根据变更前子表删除节目单子表内容
//						if (j <= 0) {
//							// 抛出异常!
//							throw new RuntimeException("变更前子表删除操作失败!");
//						} else {
//							delSum += j;// 记录总共删除的条数
//						}
//					}
					taskChangePortService.save(zcdmTChange, ListZcdmTChangesub);// 执行保存变更单主表和子表操作
					/** 根据变更后子表的内容,插入节目单子表 **/
					Date date = broadcastListChangeInfoType.getPlayDate()
							.toGregorianCalendar().getTime();// 播出日期
					String channelCode = broadcastListChangeInfoType
							.getChannelCode();// 频道代码
					List<ZcdmTProglist> zcdmTProglists = entityClassSet
							.setZcdmTProglist(date, channelCode,
									broadcastListItemTypesAfter, zcdmTProglist);
					int chanToPro = taskChangePortService
							.saveChangesubToProglist(zcdmTProglists,ListzcdmTChangesubBefore);
					if (chanToPro > 0) {
						response = "保存操作成功!";
					} else {
						response = "保存操作失败!";
					}
					_return = setResponseMessage(response,taskid);
					// 清空集合内容
					ListZcdmTChangesub.clear();
					ListzcdmTChangesubBefore.clear();
					ListzcdmTChangesubAfter.clear();
					zcdmTProglists.clear();
				}
			} else if (StringUtils
					.equals(taskBasicInfoType.getTaskType(), "CI")
					&& StringUtils.equals(requestHead.getTargetSysID(), "ES01")) {
				zppmTask = new ZppmTask();
				zppmTProginfo = new ZppmTProginfo();
				zppmTask.setZProcode(programBasicInfoType.getProgramCode());
				zppmTask.setZCapVer(programBasicInfoType.getSubtitleLanguage().getLangCode());
				zppmTask.setZAudVer(programBasicInfoType.getAudioLanguage().getLangCode());
				zppmTask.setZTaskId(changeTaskRequest.getModifiedTaskItem().get(0).getTaskID());
				zppmTask.setZTaskName(requestHead.getTargetService());
				zppmTask.setZAssignTime(taskBasicInfoType.getAssignTime());
				
				zppmTProginfo.setZProcode(programBasicInfoType.getProgramCode());
				zppmTProginfo.setZCapVer(programBasicInfoType.getSubtitleLanguage().getLangCode());
				zppmTProginfo.setZAudVer(programBasicInfoType.getAudioLanguage().getLangCode());
				zppmTProginfo.setZPgnam(programBasicInfoType.getProgramName());
				zppmTProginfo.setZSubjCd(programBasicInfoType.getOwnershipColumns());
				zppmTProginfo.setZTimeEstm(programBasicInfoType.getProgDuration().replaceAll(":", ""));
				response = taskChangePortService.updateZppmTaskProInfo(zppmTask, zppmTProginfo);
				_return = setResponseMessage(response,taskid);
			}
			return _return;
		} catch (java.lang.Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
	}
	public XMLGregorianCalendar convertToXMLGregorianCalendar(Date date) {

        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        XMLGregorianCalendar gc = null;
        try {
            gc = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
        } catch (Exception e) {

             e.printStackTrace();
        }
        return gc;
    }
	/**
	 * 
	 * @ps:设置返回信息
	 * @param response String
	 * @return
	 */
	private CommonTaskResponseType setResponseMessage(String response,String taskId) {
		CommonTaskResponseType _return = new CommonTaskResponseType();
		String code = "";
		List<CommonTaskResponseType.TaskResponseInfo> list = _return
				.getTaskResponseInfo();
		list.add(new CommonTaskResponseType.TaskResponseInfo());
		list.get(0).setDescription(response);
		list.get(0).setTaskID(taskId);

		if(response.indexOf("成功")>0){
			code = "000";
		}else if(response.indexOf("失败")>0){
			code = "050";
		}
		list.get(0).setStatusCode(code);
		_return.setResponseTimeStamp(convertToXMLGregorianCalendar(new Date()));
		return _return;
	}
}
